package com.info.pan.core.response;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;
import java.util.Objects;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResultCode<T> implements Serializable {

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 状态说明文案
     */
    private String message;

    /**
     * 返回承载
     */
    private T data;

    private ResultCode(Integer code) {
        this.code = code;
    }

    private ResultCode(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    private ResultCode(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    @JsonIgnore
    @JSONField(serialize = false)
    public boolean isSuccess() {
        return Objects.equals(this.code, ResponseEnum.SUCCESS.getCode());
    }

    public static <T> ResultCode<T> success() {
        return new ResultCode<T>(ResponseEnum.SUCCESS.getCode());
    }

    public static <T> ResultCode<T> success(String message) {
        return new ResultCode<T>(ResponseEnum.SUCCESS.getCode(), message);
    }

    public static <T> ResultCode<T> data(T data) {
        return new ResultCode<T>(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getDesc(), data);
    }

    public static <T> ResultCode<T> fail() {
        return new ResultCode<T>(ResponseEnum.ERROR.getCode());
    }

    public static <T> ResultCode<T> fail(String message) {
        return new ResultCode<T>(ResponseEnum.ERROR.getCode(), message);
    }

    public static <T> ResultCode<T> fail(Integer code, String message) {
        return new ResultCode<T>(code, message);
    }

    public static <T> ResultCode<T> fail(ResponseEnum responseCode) {
        return new ResultCode<T>(responseCode.getCode(), responseCode.getDesc());
    }
}
